package com.qimo.zuoyeoa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qimo.zuoyeoa.dao.ScheduleMapper;
import com.qimo.zuoyeoa.entity.Schedule;
import com.qimo.zuoyeoa.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleMapper scheduleMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean append(Schedule schedule) {
        return scheduleMapper.insert(schedule) > 0;
    }

    @Override
    public PageInfo<Schedule> findAll(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        final List<Schedule> schedules = scheduleMapper.selectAll();
        PageInfo<Schedule> pageInfo = new PageInfo<>(schedules);
        return pageInfo;
    }

    @Override
    public PageInfo<Schedule> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<Schedule> schedules = scheduleMapper.selectByKeyword(keyword);
        PageInfo<Schedule> pageInfo = new PageInfo<>(schedules);
        return pageInfo;
    }

    @Override
    public Schedule findById(int id) {
        return scheduleMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modify(Schedule schedule) {
        return scheduleMapper.updateById(schedule)>0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean remove(int id) {
        return scheduleMapper.deleteById(id) > 0;
    }

    @Override
    public List<Schedule> findAll() {
        return scheduleMapper.selectAll();
    }
}
